package com.htc;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * ES 配置
 *
 * @author: htc
 * @date: Created in 17:44 2018/5/18.
 */
@Configuration
public class esConfig {
    @Bean
    public TransportClient client() throws Exception {
        //ES节点地址
        InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName("localhost"),
                //这里是ES的TCP端口，而不是ES的HTTP端口
                9300
        );

        //ES配置
        Settings settings = Settings.builder()
                .put("cluster.name", "htc")
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        //添加节点地址，如果是集群，可以addTransportAddress()多次执行
        client.addTransportAddress(node);
        return client;
    }
}
